package _binary_search;

/**
 * 702. 搜索长度未知的有序数组
 */
public class No702 {
    public int search(ArrayReader reader, int target) {
        if (reader.get(0) == target) return 0;
        int right = 1;
        while (reader.get(right) < target) right <<= 1;
        int left = right >> 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (reader.get(mid) == target) return mid;
            else if (reader.get(mid) < target) left = mid + 1;
            else right = mid - 1;
        }
        return -1;
    }

    interface ArrayReader {
        default int get(int index) {
            return index;
        }
    }
}
